﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using W.Purchase.API.Application.Command.PayableCommand;
using W.Purchase.API.Application.Command.ReceivableCommand;
using W.Purchase.Domain;

namespace W.Purchase.API.Controllers
{
    /// <summary>
    /// 应付管理控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PayableController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<PayableController> _logger;
        public PayableController(IMediator mediator, ILogger<PayableController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }
        /// <summary>
        /// 添加应付管理信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> GetPayableAdd(PayableCreateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                _logger.LogInformation("调用添加应付信息");
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                var name = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Name)?.Value;//获取名称
                command.CreateName = name;
                command.user_id = Convert.ToInt32(uid);
                command.office_id = sid;
                result.Code = await _mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError("调用添加应付信息出错" + ex.Message);
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
        }
        /// <summary>
        /// 应付信息删除
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> GetPayableDelete(PayableDelCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                _logger.LogInformation("调用应付信息删除");
                result.Code = await _mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError("调用应付信息删除出错" + ex.Message);
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
        }
        /// <summary>
        /// 应付信息批量删除
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> GetPayableDeleteRange(PayableRangeDelCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                _logger.LogInformation("调用应付信息批量删除");
                result.Code = await _mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError("调用应付信息批量删除出错" + ex.Message);
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
        }
        /// <summary>
        /// 应付信息修改
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> GetPayableUpdate(PayableUpdCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                _logger.LogInformation("调用应付信息修改");
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                var name = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Name)?.Value;//获取名称
                command.UpdateName = name;
                command.user_id = Convert.ToInt32(uid);
                command.office_id = sid;
                result.Code = await _mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError("调用应付信息修改出错" + ex.Message);
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
        }
        /// <summary>
        /// 应付信息审核
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> GetPayableAudit(PayableAuditCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                _logger.LogInformation("调用应付信息审核");
                result.Code = await _mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError("调用应付信息审核出错" + ex.Message);
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
        }
        /// <summary>
        /// 新增实付
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> GetActuallpaidAdd(ActuallypaidCreateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                _logger.LogInformation("调用新增实付");
                result.Code = await _mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError("调用新增实付出错" + ex.Message);
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
        }

    }
}
